<?php
namespace app\admin\controller;
use app\BaseController;
use app\service\adminService;
use think\facade\View;

class Login extends BaseController
{
        public function login()
        {      
            $request=request();
            if($request->isGet()){
    
                return View::fetch('login');
            }else{
                $data = $request->post();
                $validate = $this->validate($data,[
                        'name|名称'	=>  'require',
                        'password|密码'	=>  'require'
                ]);
                if($validate !== true){
                    $this->error($validate);

                }
                if(!captcha_check(trim($data['captcha']))){
                    $this->error('验证码错误');
                }
                $result = (new adminService())->login($data);
                if(!empty($result)){
                    session('id',$result['id']);
                    session('name',$result['name']);
                    session("usertype",$result['usertype']);
                    return redirect(url('admin/Index/index'));
                    //$this->success("登录成功",'admin/Index/index');
                }else{
                    $this->error('登陆失败！');


                }
            }  
        }
        public function Logout(){
            session(null);
            $this->redirect(url('admin/login/login'));
        }

}